/**
 * 卫生人员注册
 * by zlh
 */
//功能栏
WsryzcBtn = Ext.extend(Ext.Toolbar,{
	constructor : function(){
		WsryzcBtn.superclass.constructor.call(this,{
			items : [{
				text : '增加',
				iconCls : 'icon-add',
				handler : this.onAdd,
				scope : this
			},'-',{
				text : '修改',
				iconCls : 'icon-edit',
				handler : this.onEdit,
				scope : this
			},'-',{
				text : '删除',
				iconCls : 'icon-remove',
				handler : this.onDelete,
				scope : this
			},'-',{
				text : '设置居民健康档案浏览权限',
				iconCls : 'icon-lock',
				handler : this.onViewRight,
				scope : this
			}]
		})
	},
	onAdd : function(){
		this.fireEvent('onAdd',this);
	},
	onEdit : function(){
		this.fireEvent('onEdit',this);
	},
	onDelete : function(){
		this.fireEvent('onDelete',this);
	},
	onViewRight : function(){
		this.fireEvent('onViewRight',this);
	}
});

//*************************************************
//添加
WsryzcInputFormPanel = Ext.extend(Ext.form.FormPanel,{
	allowBlankFont : "<font color=red>*</font>",
	constructor : function(b){
		Ext.applyIf(this, b);
		WsryzcInputFormPanel.superclass.constructor.call(this,{
			height : 350,
			width : 600,
			frame : true,
			border : false,
			autoScroll : true,
			labelAlign : 'right',
			url : '../../health/wsrySaveOrUpdate.do',
			method : 'POST',
			labelWidth : 80,
			items : [{
				layout : "column",
				border : false,
	    		items : [{
	    			columnWidth : .5,
					layout : "form",
					defaultType : 'textfield',
					border : false,
					items : [{
						xtype : 'hidden',
						name : 'ygid'
					},{
						fieldLabel : '姓名' + this.allowBlankFont,
						name : 'name',
						anchor : '90%',
						allowBlank : false
					},{
						xtype : 'textfield',
						fieldLabel : '身份证号' + this.allowBlankFont,
						regex : /(^\d{15}$)|(^\d{17}([0-9]|x|X)$)/,
						name : 'personId',
						anchor : '90%',
						allowBlank : false
					},{
						xtype : 'textfield',
						fieldLabel : '用户名' + this.allowBlankFont,
						name : 'userName',
						anchor : '90%',
						allowBlank : false
					},{
						xtype : 'textfield',
						inputType : 'password',
						fieldLabel : '密码' + this.allowBlankFont,
						name : 'password',
						anchor : '90%',
						allowBlank : false
					},{
						fieldLabel : '职务' + this.allowBlankFont,
						hiddenName : 'task',
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "local",
						triggerAction : "all",
						store : new Ext.data.SimpleStore({
							fields : ["value","text"],
							data : [["正高","正高"],["副高","副高"],["中级","中级"],
							["师级/助理","师级/助理"],["士级","士级"],["待聘","待聘"]]
						}),
						valueField : "value",
						displayField : "text",
						anchor : '90%',
						allowBlank : false
					},{ 
						fieldLabel : '人员类型',
						hiddenName : "type",
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "local",
						triggerAction : "all",
						store : new Ext.data.SimpleStore({
							fields : ["value","text"],
							data : [
								["执业医师","执业医师"],["执业助理医师","执业助理医师"],
								["见习医师","见习医师"],["注册护士","注册护士"],["助产士","助产士"],["西药师（士）","西药师（士）"],
								["中药师（士）","中药师（士）"],["检验技师（士）","检验技师（士）"],["影像技师（士）","影像技师（士）"],
								["卫生监督员","卫生监督员"],["其他卫生技术人员","其他卫生技术人员"],["其他技术人员","其他技术人员"],
								["管理人员","管理人员"],["工勤技能人员","工勤技能人员"]
							]
						}),
						valueField : "value",
						displayField : "text",
						anchor : '90%'
					},{
						fieldLabel : '业务范围',
						hiddenName : "bound",
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "local",
						triggerAction : "all",
						store : new Ext.data.SimpleStore({
							fields : ["value","text"],
							data : [
								["妇保","妇保"],["儿保","儿保"],["卫生服务","卫生服务"],
								["疾病控制","疾病控制"],["疾病管理","疾病管理"],["其它","其它"]
							]
						}),
						valueField : "value",
						displayField : "text",
						anchor : '90%'
					},{
						fieldLabel : '参加工作时间',
						xtype : 'datefield',
						format : 'Y-m-d',
					 	name : 'startWorkStr',
					 	anchor : '90%'
					}]
	    		},{
	    			columnWidth : .5,
					layout : "form",
					defaultType : 'textfield',
					border : false,
					items : [{ 
						fieldLabel : '性别' + this.allowBlankFont,
						hiddenName : "sex",
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "local",
						triggerAction : "all",
						store:new Ext.data.SimpleStore({
							fields : ["value","text"],
							data : [["男","男"],["女","女"]]
						}),
						valueField : "value",
						displayField : "text",
						allowBlank : false,
						anchor : '90%'
					},{ 
						fieldLabel : '职称' + this.allowBlankFont,
						hiddenName : "position",
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "local",
						triggerAction : "all",
						store:new Ext.data.SimpleStore({
							fields : ["value","text"],
							data : [["主任医师","主任医师"],["副主任医师","副主任医师"],["主治医师","主治医师"],
							["医师","医师"],["医士","医士"],["主任药师","主任药师"],["副主任药师","副主任药师"],
							["主管药师","主管药师"],["药师","药师"],["药士","药士"],["主任护师","主任护师"],
							["副主任护师","副主任护师"],["主管护师","主管护师"],["护师","护师"],["护士","护士"],
							["主任技师","主任技师"],["副主任技师","副主任技师"],["主管技师","主管技师"],["技师","技师"],["技士","技士"]]
						}),
						valueField : "value",
						displayField : "text",
						allowBlank : false,
						anchor : '90%'
					},{
						fieldLabel : '出生日期',
						xtype : 'datefield',
						format : 'Y-m-d',
					 	name : 'birthdayStr',
					 	anchor : '90%'
					},{ 
						fieldLabel : '学历',
						hiddenName : "education",
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "local",
						triggerAction : "all",
						store:new Ext.data.SimpleStore({
							fields : ["value","text"],
							data : [["研究生","研究生"],["大学本科","大学本科"],["大学专科及专科学校","大学专科及专科学校"],
							["中专及中技","中专及中技"],["技工学校","技工学校"],["高中","高中"],["初中及以下","初中及以下"]]
						}),
						valueField : "value",
						displayField : "text",
						anchor : '90%'
					},{ 
						xtype : 'superboxselect',
						fieldLabel : '医疗机构' + this.allowBlankFont,
						hiddenName : 'jgids',
						mode : 'remote',
						triggerAction : 'all',
						store : new Ext.data.JsonStore({
						   url : '../../health/deptList.do',
						   fields : ['text', 'value'],
						   root : 'data',
						   totalProperty : 'totalProperty'
						}),
						queryParam : 'deptName',
						minChars : 1,
						pageSize : 10,
						listWidth : 300,
						valueField : "value",
						displayField : 'text',
						listeners : {
							render : function(combo){
								if(this.flag){
									var jgids = this.record.get('jgids').split(",");
									var jgNames = this.record.get('jgNames').split(",");
									var Record = combo.getStore().recordType;
									for(var i=0;i < jgids.length;i++){
										var record = new Record();
			        					record.set("value",jgids[i]);
			        					record.set("text",jgNames[i]);
			        					combo.addRecord(record);
									}
								}
							},
							scope : this
						}
					}]
	    		}]
			},{
				layout : "form",
				border : false,
				items : [{
					fieldLabel : '奖惩经历',
					xtype : 'textarea',
					name : 'jcjlContext',
					anchor : '80%'
				},{
					fieldLabel : '教育经历',
					xtype : 'textarea',
					name : 'jyjlContext',
					anchor : '80%'
				}]
			}]
		})
	},
	clearForm : function(){
		this.getForm().reset();
	}
})
//***********************************************//
//添加
WsryzcInputWin = Ext.extend(Ext.Window,{
	wsryzcInputFormPanel : null,
	constructor : function(flag){
		this.wsryzcInputFormPanel = new WsryzcInputFormPanel(flag);
		WsryzcInputWin.superclass.constructor.call(this,{
			modal : true,
			layout : 'fit',
			buttonAlign : "center", //按钮居中
			autoHeight : true,
			width : 600,
			constrain : true,
			items : this.wsryzcInputFormPanel,
			buttons : [
			{
				text : '确定',
				iconCls : "btn-ok",
				handler : this.onSubmitClick,
				scope : this
			},'-',{
				text : '取消',
				iconCls : "btn-cancel",
				handler : function(){
					this.close();
				},
				scope : this
			}
			]
		}),
		this.addEvents('submit');
	},
   	clearWin : function(){
   		this.wsryzcInputFormPanel.clearForm();
   	},
   	onSubmitClick : function(){
   		if(this.wsryzcInputFormPanel.form.isValid())
   			this.fireEvent('submit',this,this.wsryzcInputFormPanel);
   		else{
   			var values = this.wsryzcInputFormPanel.getForm().getValues();
   			Ext.ux.Toast.msg('提示信息',"表单验证不通过");
   		}
   			
   	},
	load : function(_record){
		this.wsryzcInputFormPanel.getForm().loadRecord(_record);
		this.show();
	}
});
//*********************************
//修改WIN
AddWsryzcWin = Ext.extend(WsryzcInputWin,{
	title :　"添加医务人员"
});
//修改WIN
UpdateWsryzcWin = Ext.extend(WsryzcInputWin,{
	title :　"修改医务人员"
});
// 查询的工具栏
WsryzcSearchTbar = Ext.extend(Ext.Toolbar, {
	recordSortStore : null,
	constructor : function() {
		this.recordSortStore = this.getRecordSortStore;
		WsryzcSearchTbar.superclass.constructor.call(this, {
			items : ['姓名：', {
						xtype : 'textfield',
						name : 'name',
						width : 100
					},'-','所属机构：',{
						xtype : 'textfield',
						name : 'jgNames',
						width : 100
					}, '-', {
						text : "查询",
						iconCls : 'icon-search',
						tooltip : '查询记录',
						handler : function() {
			        		this.onSerchClick();
						},
						scope : this
					}]
		})
	},
	onSerchClick : function() {
		this.fireEvent('onSerchClick', this);
	},
	getRecordSortStore : new Ext.data.JsonStore({
		url : 'sys/recordSort/listCombo.action',
		method : 'POST',
		fields : ['value','text']
	})
});
WsryzcSearchPanel = Ext.extend(Ext.form.FormPanel, {
	wsryzcSearchTbar : null,
	constructor : function() {
		this.wsryzcSearchTbar = new WsryzcSearchTbar();
		WsryzcSearchPanel.superclass.constructor.call(this, {
			defaultType : 'textfield',
			border : false,
			frame : false,
			items : [this.wsryzcSearchTbar]
		})
	}
});
WsryzcGridPanel = Ext.extend(Ext.grid.GridPanel,{
	addWsryzcWin : null,
	updateWsryzcWin : null,
	wsryzcBtn : null,
	wsryzcSearchPanel : null,
	wsryzcStore : null,
	wsryzcCSM : null,
	wsryzcCM : null,
	constructor : function(){
		this.wsryzcBtn = new WsryzcBtn();
		this.wsryzcSearchPanel = new WsryzcSearchPanel();
		this.initUIComponents();
		WsryzcGridPanel.superclass.constructor.call(this,{
			title : '▪ 您现在所在的位置：卫生人员注册',
			border : true,
			renderTo : Ext.getBody(),
			viewConfig : {
				forceFit : true
			},
			bodyStyle : 'padding:5px 5px 5px 5px',
			stripeRows : true,//斑马线
			tbar : this.wsryzcSearchPanel,
			colModel : this.wsryzcCM,
			store : this.wsryzcStore,
			sm : this.wsryzcCSM,
			bbar : new Ext.PagingToolbar({
				pageSize : 20,
				store : this.wsryzcStore,
				displayInfo : true,
				displayMsg : "显示第{0}-{1}条记录，一共{2}条记录",
				emptyMsg : "没有记录"
			})
		}),
		this.wsryzcBtn.on('onAdd',this.onAdd,this);
		this.wsryzcBtn.on('onEdit',this.onEdit,this);
		this.wsryzcBtn.on('onDelete',this.onDelete,this);
		this.wsryzcBtn.on('onViewRight',this.onViewRight,this);
		this.wsryzcSearchPanel.wsryzcSearchTbar.on('onSerchClick',this.loadList,this);
		this.loadList();
	},
	initUIComponents : function(){
		this.wsryzcCSM = new Ext.grid.CheckboxSelectionModel();
		this.wsryzcStore = new Ext.data.JsonStore({
			url : '../../health/wsryList.do',
			method : 'POST',
			totalProperty : 'totalProperty',
			root : 'data',
			fields : ["ygid","name","sex","userName","password","task","position","type","birthdayStr",
				"bound","education","startWorkStr","regTime","curState","jgids",
				"jcjlContext","jyjlContext","personId","jgNames"]
		});
		this.wsryzcCM = new Ext.grid.ColumnModel([this.wsryzcCSM,{
			header : '姓名',
			dataIndex : 'name',
			align : 'center'
		},{
			header : '性别',
			dataIndex : 'sex',
			align : 'center'
		},{
			header : '职务',
			dataIndex : 'task',
			align : 'center'
		},{
			header : '职称',
			dataIndex : 'position',
			align : 'center'
		},{
			header : '所属机构',
			width : 150,
			dataIndex : 'jgNames',
			align : 'center'
		},{
			header : '人员类型',
			dataIndex : 'type',
			align : 'center'
		},{
			header : '学历',
			dataIndex : 'education',
			align : 'center'
		},{
			header : '注册时间',
			dataIndex : 'regTime',
			align : 'center'
		}]);
	},
	listeners : {
		'render' : function() {
			this.wsryzcBtn.render(this.tbar);
		}  
	},
	loadList : function(_searchPanel){
		if(_searchPanel != null){
			var values = this.wsryzcSearchPanel.getForm().getValues();
			this.getStore().baseParams={
				name : values.name,
				jgNames : values.jgNames
			}
		}
		this.getStore().load({
			params : {
				start : 0,
				limit : 20
			}
		});
	},
	insertUpdateRecord : function(_win,_form){
		var self = this;
		_form.form.submit({
			waitTitle : "请稍等",
			waitMsg : "正在保存您的信息",
			success : function(form,action){
				if(action.result.success){
					Ext.ux.Toast.msg('提示信息',"保存成功！");
					_win.close();
					self.getStore().reload();
				}
			},
			failure : function(form, action) {
				if(action.result == null){
					Ext.ux.Toast.msg('提示信息',"保存信息失败,请联系管理员！");
				}else{
					Ext.ux.Toast.msg('提示信息',action.result.msg);
				}
			}
		});
	},
	onAdd : function(){
		this.addWsryzcWin = new AddWsryzcWin(false);
		this.addWsryzcWin.mon(this.addWsryzcWin,'submit',this.insertUpdateRecord,this);
		this.addWsryzcWin.show();
	},
	onEdit : function(){
		if (this.getSelectionModel().getCount() == 0) { // 一条都没选
			Ext.ux.Toast.msg('信息提示','您未选择任何记录，请选择要修改的记录!');
		}else if(this.getSelectionModel().getCount() > 1){
			Ext.ux.Toast.msg('信息提示','您选择了多条记录，一次只能修改一条记录!');
		}else{
			var record = this.getSelectionModel().getSelected();
			this.updateWsryzcWin = new UpdateWsryzcWin({
				flag : true,
				record :　record
			});
			this.updateWsryzcWin.mon(this.updateWsryzcWin,'submit',this.insertUpdateRecord,this);
			this.updateWsryzcWin.load(record);
		}
	},
	onDelete : function(){
		if (this.getSelectionModel().getCount() == 0) { // 一条都没选
			Ext.ux.Toast.msg('信息提示','您未选择任何记录，请选择要修改的记录!');
		}else{
			var self = this;
			var records = this.getSelectionModel().getSelections();
			var ids = new Array(records.length);
			for(var i =0;i < records.length ; i++){
				ids[i] = records[i].get('ygid');
			}
			Ext.MessageBox.confirm('删除确认', '您确认要删除记录吗？按是将删除选中的记录！',
				function(btn) {
					if(btn=='no'){
						return;
					}
					self.del(ids,records);
				}
			)
		}
	},
	del : function(ids,records){
		var self = this;
		Ext.Ajax.request({
			url : '../../health/wsryDel.do',
			params : {
				ids : ids
			},
			method : "post",
			success : function(response) {
				var result = Ext.decode(response.responseText);
				if(result.success){
					Ext.ux.Toast.msg('提示信息','删除成功!');
					self.getStore().reload();
				}else{
					Ext.ux.Toast.msg('提示信息','删除失败，请联系管理员!');
				}
			},
			failure : function(form, action) {
				Ext.ux.Toast.msg('提示信息',"删除失败,请联系管理员!");
			}
		});
	},
	onViewRight : function(){//设置居民健康档案浏览权限
		if (this.getSelectionModel().getCount() == 0) { // 一条都没选
			Ext.ux.Toast.msg('信息提示','您未选择任何记录，请选择要操作的记录!');
		}else if(this.getSelectionModel().getCount() > 1){
			Ext.ux.Toast.msg('信息提示','您选择了多条记录，一次只能操作一条记录!');
		}else{
			var record = this.getSelectionModel().getSelected();
			var ygid = record.get('ygid');
			new Szjmjkdallqx(ygid).show();
		}
	}
});
